//
//  DJShopMainViewController.m
//  Adorable_Things
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "DJShopMainViewController.h"
#import "DJCommentViewController.h"
#import "DJAttentionViewController.h"
#import "ATLoginViewController.h"

@interface DJShopMainViewController ()

@property (nonatomic, strong) DJCommentViewController *commentVC;
@property (nonatomic, strong) DJAttentionViewController *attentionVC;
@property (nonatomic, strong) UILabel *leftLab;
@property (nonatomic, strong) UILabel *rightLab;
@property (nonatomic, strong) UISegmentedControl *seg;

@end

@implementation DJShopMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBarHidden = YES;
    
    self.commentVC = [[DJCommentViewController alloc]initWithNibName:@"DJCommentViewController" bundle:[NSBundle mainBundle]];
    [self addChildViewController:self.commentVC];
    [self.view addSubview:self.commentVC.view];
    
    self.attentionVC = [[DJAttentionViewController alloc]init];
    [self addChildViewController:self.attentionVC];
    
    self.seg = [[UISegmentedControl alloc]initWithItems:@[@"推荐",@"关注"]];
    self.seg.frame = CGRectMake(kWidth / 3, 20, kWidth / 3, 30);
    self.seg.selectedSegmentIndex = 0;
    self.seg.backgroundColor = [UIColor whiteColor];
    // 设置未选中颜色
    NSDictionary * unselectedTextAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor grayColor]};
    [self.seg setTitleTextAttributes:unselectedTextAttr forState:UIControlStateNormal];
    
    // 设置选中颜色
    NSDictionary * selectedTextAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor redColor]};
    [self.seg setTitleTextAttributes:selectedTextAttr forState:UIControlStateSelected];
    self.seg.tintColor = [UIColor whiteColor];
    
    [self.view addSubview:self.seg];
    
    self.leftLab = [[UILabel alloc]initWithFrame:CGRectMake(kWidth / 2 - 48, 48, 35, 2)];
    self.leftLab.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.leftLab];
    
    self.rightLab = [[UILabel alloc]initWithFrame:CGRectMake(kWidth / 2 + 12, 48, 35, 2)];
    self.rightLab.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.rightLab];

    
    [self.seg addTarget:self action:@selector(segmentView:) forControlEvents:(UIControlEventValueChanged)];
    
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];
    
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRightAction:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRight];
    
}

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    self.seg.selectedSegmentIndex = 1;
    [self segmentView:self.seg];
   
}

- (void)swipeRightAction:(UISwipeGestureRecognizer *)swipe
{
    
    self.seg.selectedSegmentIndex = 0;
    [self segmentView:self.seg];
    
}

- (void)segmentView:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        [UIView transitionFromView:self.attentionVC.view toView:self.commentVC.view duration:0.5 options:(UIViewAnimationOptionTransitionCrossDissolve) completion:nil];
        self.leftLab.backgroundColor = [UIColor redColor];
        self.rightLab.backgroundColor = [UIColor whiteColor];
    }else{
        ATUserManager *manager = [ATUserManager shareManager];
        if (manager.isLogin != 1) {
            ATLoginViewController *loginVC = [[ATLoginViewController alloc]init];
            UINavigationController *loginNav = [[UINavigationController alloc]initWithRootViewController:loginVC];
            loginNav.navigationBarHidden = YES;
            [self presentViewController:loginNav animated:YES completion:nil];
        }
        [UIView transitionFromView:self.commentVC.view toView:self.attentionVC.view duration:0.5 options:(UIViewAnimationOptionTransitionCrossDissolve) completion:nil];
        self.leftLab.backgroundColor = [UIColor whiteColor];
        self.rightLab.backgroundColor = [UIColor redColor];
        
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
